package net.xxcl.stuManager.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.xxcl.stuManager.mapper.DBSessionFactory;
import net.xxcl.stuManager.mapper.PoorMapper;
import net.xxcl.stuManager.mapper.StudentMapper;
import net.xxcl.stuManager.pojo.Poor;
import net.xxcl.stuManager.pojo.Student;
import net.xxcl.stuManager.pojo.Teacher;
import net.xxcl.stuManager.util.DigestUtil;

public class PoorAction extends HttpServlet {

	private PoorMapper poorMapper = new PoorMapper();
	private StudentMapper studentMapper = new StudentMapper();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(request.getParameter("action").equalsIgnoreCase("edit")){
			edit(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("add")){
			add(request, response);
		}else if (request.getParameter("action").equalsIgnoreCase("polist")){
			polist(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("selectById")){
			getByid(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("del")){
			del(request, response) ;
		
		}
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		
	}
	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 * 根据id删除学生贫困建档信息
	 */
	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
				
		Map<String,Object> map = new HashMap<String, Object>();
		
		try {
			String ids = request.getParameter("ids");
			String[] temp = ids.split(","); 
			for (int i = 0; i < temp.length; i++) {
				poorMapper.deleteById(Integer.parseInt(temp[i]));
			}
			map.put("success", true);
			map.put("msg", "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "删除失败！");
		}		
	
		JSONObject json = JSONObject.fromObject(map);
		out.print(json);
		out.close();
	}
	/**
	 * 新增学生贫困建档
	 * @throws IOException 
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {		

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		try {
			Poor poor  = new Poor();
			poor.setStudentid(Integer.parseInt(request.getParameter("studentid")));
			poor.setFamnumber(request.getParameter("famnumber"));
			poor.setAnnincome(request.getParameter("annincome"));
			poor.setRemarks(request.getParameter("remarks"));
			poor.setPovlevel(request.getParameter("povlevel"));
			Teacher teacher=(Teacher)request.getSession().getAttribute("teacher");
			poor.setTeacherid(teacher.getId());
			poorMapper.save(poor);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "新增学生贫困建档信息失败！");
		}		
	
		JSONObject json = JSONObject.fromObject(map);
		out.print(json);
		out.close();
	}
	/**
	 * 辅导员可以管理的贫困生建档列表
	 * @throws IOException 
	 */
private void polist(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		//前台传来的分页信息
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		
		Teacher teacher = (Teacher)request.getSession().getAttribute("teacher");
		
		//设置sql语句所需要的参数
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("teacherid", teacher.getId());
		map.put("start", (page-1)*rows);
		map.put("count", rows);
		
		if(request.getParameter("queryName")!=null){
			map.put("queryName", "%"+request.getParameter("queryName")+"%");
		}
		
		List<Poor> list =poorMapper.selectpoListByTeacherid(map);
		
		int total = poorMapper.countpoByTeacherid(map);
		//设置传到前台的信息
		Map<String,Object> responeMap = new HashMap<String, Object>();
		responeMap.put("total", total);
		responeMap.put("rows", list);
		JSONObject json = JSONObject.fromObject(responeMap);
		out.print(json);
		out.close();
	}
/**
 * @param request
 * @param response
 * @throws IOException
 * 根据id查找学生贫困建档信息
 */
private void getByid(HttpServletRequest request, HttpServletResponse response) throws IOException {		

	response.setContentType("text/html");
	response.setCharacterEncoding("utf-8");
	PrintWriter out = response.getWriter();
	
	int id = Integer.parseInt(request.getParameter("id"));
	Poor poor = poorMapper.selectById(id);
	

	JSONObject json = JSONObject.fromObject(poor);
	out.print(json);
	out.close();
}
/**
 * 修改一条学生贫困建档信息
 * @throws IOException 
 */
private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {		

	response.setContentType("text/html");
	response.setCharacterEncoding("utf-8");
	PrintWriter out = response.getWriter();
	
	Map<String,Object> map = new HashMap<String, Object>();
	
	try {
		Poor poor  = new Poor();
		poor.setId(Integer.parseInt(request.getParameter("id")));
		poor.setStudentid(Integer.parseInt(request.getParameter("studentid")));
		poor.setFamnumber(request.getParameter("famnumber"));
		poor.setAnnincome(request.getParameter("annincome"));
		poor.setRemarks(request.getParameter("remarks"));
		poor.setPovlevel(request.getParameter("povlevel"));
		poorMapper.update(poor);
		map.put("success", true);
	} catch (Exception e) {
		e.printStackTrace();
		map.put("msg", "修改学生贫困建档信息失败！");
	}		

	JSONObject json = JSONObject.fromObject(map);
	out.print(json);
	out.close();
}
	

}
